package com.yidu.best.systemManagement.userControl.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yidu.best.systemManagement.userControl.dao.UserDao;
import com.yidu.best.systemManagement.userControl.domain.User;
import com.yidu.best.systemManagement.userControl.domain.UserVo;
import com.yidu.best.systemManagement.userControl.service.UserService;
import com.yidu.best.systemManagement.userControl.util.IDGenerator;
/**
 * 用户逻辑实现类
 * @author 阳雄
 * @DateTime 2020年1月9日12:05:14
 */
@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	
	public UserServiceImpl() {
		super();
	}

	public UserDao getUserDao() {
		return userDao;
	}


	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


	public UserServiceImpl(UserDao userDao) {
		super();
		this.userDao = userDao;
	}

	/**
	 * 显示用户所有数据信息
	 */
	@Override
	public Map<String, Object> queryUserByCriteriaWithPage(UserVo userVo) {
		 int page = (userVo.getPage()-1)*userVo.getLimit(); userVo.setPage(page);
			List<User> pageByParam = userDao.selectPageParam(userVo);
			int count = userDao.selectCount(userVo);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("data",pageByParam);
			map.put("count", count);
			map.put("msg", "");
			map.put("code", 0);
			return map;
	}

	@Override
	public int addUser(User user) {		
		user.setUserId(IDGenerator.getId());
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		return userDao.addUser(user);
	}	

	@Override
	public int updateUser(User userId) {
	
		
		return userDao.updateUser(userId);
	}

	@Override
	public int deleteUser(String userIdStr) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String[] userIds = userIdStr.split(",");
		int total = 0;
		for (int i = 0; i < userIds.length; i++) {
			paramMap.put("userId", userIds[i]);
			int result = userDao.deleteById(paramMap);
			total += result;
		}
		return total;
	}

	@Override
	public int selectCount(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int logicDelete(User user) {
		return userDao.logicDelete(user);
	}

	@Override
	public User login(String userName, String password) {
		String	md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
		User paramUser = new User(userName,md5Password);
		User user = userDao.login(paramUser);
		if(user!=null){
			return user;
		}
		return null;
	}

	@Override
	public User selectUserName(String userName) {
		User selectUserName = userDao.selectUserName(userName);
		return selectUserName;
	}

	 
}
